//
//  HROrderApplyListViewController.m
//  haierapp
//
//  Created by gaotangzhang on 14-9-5.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HROrderApplyListViewController.h"

#import "HROrderApplyList.h"

#import "HROrderApplyInfoViewController.h"

#define HEIGHT_HEADER_CELL 40.0f

#define HEIGHT_CELL 80.0f

@interface HROrderApplyListViewController (){
    NSArray *orderArray;

}

@end

@implementation HROrderApplyListViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.title=@"售后订单";
    
    
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    //去除ios7上table的多余分割线
    UIView *footerView=[[UIView alloc]init];
    footerView.backgroundColor=[UIColor clearColor];
    [self.tableView setTableFooterView:footerView];
    
    //加载订单数据
    [self loadOrderListData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Methods
//加载订单数据
-(void)loadOrderListData{
    
    //隐藏无数据提示页面
    [self hideHud];
    
    [self showCustomLoadingHud:kLoading inView:nil];
    
    [HRInterfaceManager getOrderListApply:^(BOOL isSucceed, NSString *message, id data) {
        
        [self hideCustomLoadingHud];
        
        if (isSucceed) {
            HROrderApplyList *orderList=(HROrderApplyList *)data;
            orderArray=orderList.list;
            
            [self.tableView reloadData];
            
        }else if([message isEqualToString:NO_WIFI_ERROR_CODE]){
            //显示无数据加载页面
            [self showHudByType:HUDTYPE_NO_WIFI];
            
            return;
        }else{
            [self showTextHUD:message];
        }
        
        if (orderArray==nil || [orderArray count]==0) {
            [self showHudByType:HUDTYPE_ORDER_RETURN];
        }
        
        //页面加载耗时
        [self analysisEnd:nil];
        
    }];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [orderArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    HROrderApply *order=[orderArray objectAtIndex:section];
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"orderapplycell";
    HROrderApplyCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HROrderApplyCell alloc] init];
    }
    
    HROrderApply *order=[orderArray objectAtIndex:indexPath.section];
    HROrderProductInfo *product=order.productInfo;
    
    [cell configWithData:product];
    
    //是否显示底部间隔线
    int count=1;
    
    [cell setShowBottomLine:NO];
    
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    HROrderApply *order=[orderArray objectAtIndex:section];
    
    HROrderApplyHeader *headerView=[[HROrderApplyHeader alloc]initWithType:APPLY_HEADER_TITLE_DESC showTopLine:YES];
    
    headerView.titleLabel.text=[NSString stringWithFormat:@"服务号:%@",order.serviceNo];
    
    headerView.descLabel.text=@"";
    
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    HROrderApply *order=[orderArray objectAtIndex:section];
    
    HROrderApplyFooter *footerView=[[HROrderApplyFooter alloc]initWithShowBottomLine:YES AndShowSeperateView:YES];
    footerView.delegate=self;
    footerView.orderNo=order.orderNo;
    
    HROrderProductInfo *product=order.productInfo;
    
    int goodsCount=1;
    
    if (product.goodsAmount!=nil && [product.goodsAmount intValue]>0) {
        goodsCount=[product.goodsAmount intValue];
    }
    
    footerView.goodsCountLabel.text=[NSString stringWithFormat:@"共%d件商品",goodsCount];
    
    CGFloat price=[product.unitPrice floatValue] * goodsCount;
    
    footerView.goodsTotalPriceLabel.text=[NSString stringWithFormat:@"￥%.2f",price];
    
    return footerView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT_CELL;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return HEIGHT_HEADER_CELL;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return HEIGHT_FOOTER_ORDERCELL_SHOW_SEPERATE;
}

#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HROrderApply *order=[orderArray objectAtIndex:indexPath.section];
    
    //查看售后详情页面
    HROrderApplyInfoViewController *infoViewController=[[HROrderApplyInfoViewController alloc]initWithNibName:NIBNAME_ORDER_REFUND_AND_AFTER_SALE_APPLY_INFO bundle:nil];
    infoViewController.serviceNo=order.serviceNo;
    
    [self.navigationController pushViewController:infoViewController animated:YES];
   
}


#pragma mark - HRProgressHUDDelegate Methods

-(void)tapHUDButtonAction:(id)button{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
